package com.duke.shaking.widget;

import com.duke.shaking.Constants;
import com.duke.shaking.R;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Handler;
import android.os.Message;
import android.view.View;

public class StartTextEditActivityHandler extends Handler {
	private Context context;
	
	public StartTextEditActivityHandler(Context context) {
		this.context = context;
	}

	@Override
	public void handleMessage(Message msg) {
		String original = (String)msg.obj;
		int titleId = -1;
		boolean multiLine = false;
		if(msg.what == Constants.INTENT_CODE_EDIT_SCHOOL) {
			titleId = R.string.school;
			multiLine = true;
		}
		else if(msg.what == Constants.INTENT_CODE_EDIT_NICKNAME) {
			titleId = R.string.nickname;
			multiLine = false;
		}
		else if(msg.what == Constants.INTENT_CODE_EDIT_SIGNATURE) {
			titleId = R.string.signature;
			multiLine = true;
		}
		
		if(titleId != -1) {
			openTextEdit(titleId, 0, original, multiLine, msg.what);
		}
	}
	
	private void openTextEdit(int titleId, 
			int tipId, 
			String original, 
			boolean multiLine,
			int requestCode) {
		Intent i = new Intent(this.context, TextEditActivity.class);
		i.putExtra("title", titleId);
		i.putExtra("tip", tipId);
		i.putExtra("multiLine", multiLine);
		if (original != null) {
			i.putExtra("original", original);
		}
		i.putExtra("editCode", requestCode);
		if(context instanceof Activity) {
			((Activity)this.context).startActivityForResult(i, requestCode);
		} else {
			i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
			context.startActivity(i);
		}
	}
}
